﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enterprise.Framework.Core.Container;
using Enterprise.Framework.DomainModel;
using Enterprise.Framework.Core.Exceptions;
using Enterprise.Framework.DomainModel.Specifications;
using Resto.CostControl.WebControl.DWZAdapter;
using Resto.CostControl.Core.Query.MaterialModule.QueryObject;
using Resto.CostControl.Core.Query.MaterialModule.ViewObject;
using Resto.CostControl.Core.Facade.MaterialModule.DataTransferObject;
using Resto.CostControl.Core.Facade.MaterialModule.FacadeObject;
using Resto.CostControl.Core.Enums;
using Resto.CostControl.Core;
using Resto.CostControl.WebControl.Service;

namespace Resto.CostControl.Web.Controllers
{
    public class MaterialController : BaseController
    {
        public ActionResult Index(DWZPageInfo pageInfo)
        {
            MaterialQO materialQO = ObjectCreator.Create<MaterialQO>();
            PagerCondition pager = pageInfo.ToPager();
            ISpecification<MaterialVO> spec = Specification<MaterialVO>.Create(p => p.ID != null);
            if (!string.IsNullOrWhiteSpace(pageInfo.Keywords))
            {
                spec = spec.And(Specification<MaterialVO>.Create(p => p.Name == pageInfo.Keywords));
            }

            MaterialVO[] array = materialQO.QueryMaterialVOByPager(spec, pager, "Name", true);

            ViewBag.Total = pager.RecordCount;
            ViewBag.PageNum = pageInfo.PageIndex;
            ViewBag.NumPerPage = pageInfo.PageSize;
            ViewBag.OrderField = "Name";
            ViewBag.OrderDirection = "asc";
            //ViewBag.Keywords = pageInfo.Keywords;
            //ViewBag.SearchData = pageInfo.SearchData;

            return View(array);
        }

        //    //
        //    // GET: /Material/

        //    public ActionResult Index(DWZPageInfo pageInfo, QueryModel where)
        //    {
        //        var materials = _RepoMaterial.GetPageable(pageInfo, where);

        //        ViewBag.Total = materials.Total;
        //        ViewBag.PageNum = pageInfo.PageIndex + 1;
        //        ViewBag.NumPerPage = pageInfo.PageSize;
        //        ViewBag.OrderField = pageInfo.SortName;
        //        ViewBag.OrderDirection = pageInfo.SortOrder;
        //        ViewBag.Keywords = pageInfo.Keywords;
        //        ViewBag.SearchData = pageInfo.SearchData;

        //        return View(materials.Page);
        //    }

        public ActionResult Edit(string Id)
        {
            MaterialDTO data = null;
            if (Id != null)
            {
                MaterialFO materialFO = ObjectCreator.Create<MaterialFO>();

                data = materialFO.GetMaterialDTO(Id);
            }
            else
            {
                data = new MaterialDTO();
            }
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(MaterialDTO data)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    MaterialFO materialFO = ObjectCreator.Create<MaterialFO>();
                    if (!string.IsNullOrWhiteSpace(data.ID))
                        materialFO.UpdateMaterial(data);
                    else
                        materialFO.AddMaterial(data);

                }
                catch (BusinessValidationException ex)
                {
                    throw ex;
                }
            }

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "Material",
                callbackType = "closeCurrent",
                forwardUrl = "",
            };
            return Json(result);
        }

        public ActionResult LookupMaterial(DWZPageInfo pageInfo)
        {
            MaterialQO materialQO = ObjectCreator.Create<MaterialQO>();
            PagerCondition pager = pageInfo.ToPager();

            ISpecification<MaterialVO> spec = Specification<MaterialVO>.Create(p => p.ID != null);
            if (!string.IsNullOrWhiteSpace(pageInfo.Keywords))
            {
                spec = spec.And(Specification<MaterialVO>.Create(p => p.Name == pageInfo.Keywords));
            }

            MaterialVO[] array = materialQO.QueryMaterialVOByPager(spec, pager, "Name", true);

            ViewBag.Total = pager.RecordCount;
            ViewBag.PageNum = pageInfo.PageIndex;
            ViewBag.NumPerPage = pageInfo.PageSize;
            ViewBag.OrderField = "Name";
            ViewBag.OrderDirection = "asc";

            return View(array);
        }

    }

    /// <summary>
    /// 每页显示数量的实体
    /// </summary>
    public class PageSize
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }

    /// <summary>
    /// Ajax表单返回结果
    /// </summary>
    public class FormReturnJson
    {
        public string statusCode { get; set; }
        public string message { get; set; }
        public string navTabId { get; set; }
        public string callbackType { get; set; }
        public string forwardUrl { get; set; }
    }
}
